const KEY_PREFIX = "account_auth_sdk:"

const getStoreKey = (key: string) => {
    return KEY_PREFIX + key
}
export const createStore = (persistence: boolean) => {
    const storage: Storage = persistence ? localStorage : sessionStorage

    const save = (key: string, value: string): void => {
        key = getStoreKey(key)
        storage.setItem(key, value)
    }
    const remove = (key: string): void => {
        key = getStoreKey(key)
        storage.removeItem(key)
    }
    const get = (key: string): string => {
        key = getStoreKey(key)
        return storage.getItem(key)
    }
    const clear = (): void => {
        for (let key in storage) {
            if (key.startsWith(KEY_PREFIX)) {
                storage.removeItem(key)
            }
        }
    }

    return {
        get,
        save,
        remove,
        clear
    }
}